import sys
import os
d = os.path.dirname(__file__)
parent_path = os.path.dirname(d)
sys.path.append(parent_path)
from ulits import fstool
import time
import datetime

###生成订单~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class Gener_ord(object):
    def __init__(self, buyer_id, blag):
        self.buyer_id=buyer_id
        self.blag=blag
    def gener(self):
        if self.blag:
            
            times=''.join(str(datetime.datetime.now())[:10].split('-'))
            excel=fstool.Excel(parent_path+'/data/goods.xls')
            red_data=excel.read_all_data('goods')
            buyer_car=[]
            for i in red_data:
                if self.buyer_id in i:
                    buyer_car.append(i)
            # print(buyer_car)
            ord_all_excel=fstool.Excel(parent_path+'/data/order_goods.xls')
            ord_buy_excel=fstool.Excel(parent_path+'/data/order_buyer.xls')
            ord_sell_excel=fstool.Excel(parent_path+'/data/order_seller.xls')
            buy_excel=fstool.Excel(parent_path+'/data/buyer.xls')
            all_buy_data=buy_excel.read_all_data('buyer')
            for buy in all_buy_data:
                if self.buyer_id in buy:
                    buy_data=buy
            for i in buyer_car:#写入订单
                get_time=str(time.time())[:10]
                time.sleep(1.1)
                goods_ord=[]
                buyer_ord=[]
                sell_ord=[]
                ord_id=i[1][-4:]+get_time
                goods_ord.append(ord_id)
                goods_ord.extend(i[-3:])
                goods_ord.append('待发货')
                goods_ord.append('待发货')
                goods_ord.append(times)
                ord_all_excel.write_row_data(goods_ord,'order_goods')
                # print(goods_ord)
                #写如总订单~~~~
                sell_ord.append(ord_id)
                sell_ord.extend(i[2:5])
                god_money=int(i[-1])*int(i[-2])
                sell_ord.append(str(god_money))
                sell_ord.append('待发货')
                # print(sell_ord)
                ord_sell_excel.write_row_data(sell_ord,'order_seller')
                #写卖家订单
                buyer_ord.append(ord_id)
                buyer_ord.extend(buy_data[:2])
                buyer_ord.append(buy_data[-1])
                buyer_ord.append(str(god_money))
                buyer_ord.append('待发货')
                # print(buyer_ord)
                ord_buy_excel.write_row_data(buyer_ord,'order_buyer')
                #写买家订单
            store_excel = fstool.Excel(parent_path + '/data/store.xls')
            all_data = store_excel.read_all_data('store')
            for car1 in buyer_car:
                goods_num = 0
                for j in all_data:
                    if car1[0] in j:
                        goods_num = j[2]
                goods_num = int(goods_num) - int(car1[6])
                dict0 = {'ID':int(car1[0]),'商品数量':str(goods_num)}
                store_excel.write_cell_data(dict0,'store')





            excel.delete_all_data('goods')
            print('生成订单成功')
        else:
            return('您还未支付')


if __name__=='__main__':
    c=Gener_ord('12345678901',True)
    c.gener()